/*
 * @Author: 码上talk|RC
 * @Date: 2020-06-13 10:00:35
 * @LastEditTime: 2021-01-05 16:22:44
 * @LastEditors: 码上talk|RC
 * @Description: 
 * @FilePath: /tacomall-merchant/src/model/goods/index.js
 * @Just do what I think it is right
 */
/*eslint-disable */
import { GoodsItem } from './item'
import api from '@/api'
import { formToolkit } from '@/utils/form-toolkit'

export class Goods {
    constructor(goods = {
        goodsItem: []
    }) {
        const { id, name, amount, cover, goodsItem, stock } = goods
        this.id = id
        this.name = name
        this.amount = amount
        this.cover = cover
        this.items = goodsItem.map(i => new GoodsItem(i))
        this.status = '出售中'
        this.stock = 1000
    }
}

Goods.ENUM = {
    init: () => {
        return {
            goodsCategory: [],
            goodsBrand: []
        }
    },
    load: async () => {
        let result = {}
        for (let i in Goods.ENUM.init()) {
            let body = {}
            if (i === 'goodsCategory') {
                const query = {
                    pId: 0
                }
                body['query'] = query
            }
            const res = await api.open.enumLst({ key: i }, body)
            let { status, data } = res
            if (status) {
                result[i] = data.map(j => {
                    let json = {
                        value: j['id'],
                        label: j['name']
                    }
                    if (i === 'goodsCategory') {
                        json = {
                            value: j['id'],
                            label: j['name'],
                            children: [],
                            loading: false
                        }
                    }
                    return json
                })
            }
        }
        return result
    },
    lazyLoad: async (key = 'goodsCategory', body, config = {}) => {
        const res = await api.open.enumLst({ key }, body)
        let { status, data } = res
        if (status) {
            if (key === 'goodsCategory') {
                config['item'].children = data.map(i => {
                    let json = {
                        value: i['id'],
                        label: i['name'],
                        children: [],
                        loading: false
                    }
                    if (!i.isHasChildren) {
                        json = {
                            value: i['id'],
                            label: i['name'],
                        }
                    }
                    return json
                })
                config['item'].loading = false
                config['callback']()
            }
        }
    }
}

Goods.FORM = {
    init: (type = 'add') => {
        switch (type) {
            case 'add':
                return {
                    category: {
                        isRequire: true,
                        type: 'string|min:2',
                        key: 'goodsCategoryId',
                        value: null
                    },
                    brand: {
                        isRequire: true,
                        type: 'int',
                        key: 'goodsBrandId',
                        value: null
                    },
                    name: {
                        isRequire: true,
                        type: 'string|min:4|max:12',
                        key: 'name',
                        value: null
                    },
                    title: {
                        isRequire: false,
                        type: 'string|min:4|max:12',
                        key: 'title',
                        value: ''
                    },
                    cover: {
                        isRequire: true,
                        type: 'string',
                        key: 'cover',
                        value: ''
                    },
                    marketAmount: {
                        isRequire: true,
                        type: 'int',
                        key: 'marketAmount',
                        value: 0
                    },
                    amount: {
                        isRequire: true,
                        type: 'int',
                        key: 'amount',
                        value: 0
                    },
                    recommond: {
                        isRequire: false,
                        type: 'int',
                        key: 'recommond',
                        value: 1
                    },
                    goodsItem: {
                        isRequire: true,
                        type: 'array>GoodsItem',
                        key: 'goodsItem',
                        value: []
                    }
                }
            case 'update':
                return {
                    id: {
                        isRequire: true,
                        type: 'int',
                        key: 'id',
                        value: null
                    },
                    category: {
                        isRequire: true,
                        type: 'string|min:2',
                        key: 'goodsCategoryId',
                        value: null
                    },
                    brand: {
                        isRequire: true,
                        type: 'int',
                        key: 'goodsBrandId',
                        value: null
                    },
                    name: {
                        isRequire: true,
                        type: 'string|min:4|max:12',
                        key: 'name',
                        value: null
                    },
                    title: {
                        isRequire: false,
                        type: 'string|min:4|max:12',
                        key: 'title',
                        value: ''
                    },
                    cover: {
                        type: 'string',
                        key: 'cover',
                        value: '//img10.360buyimg.com/n1/s450x450_jfs/t1/134887/24/15636/112103/5faa2eb9Eb1fe99ef/2ff0882c9607e57c.jpg'
                    },
                    marketAmount: {
                        isRequire: true,
                        type: 'int',
                        key: 'marketAmount',
                        value: 0
                    },
                    amount: {
                        isRequire: true,
                        type: 'int',
                        key: 'amount',
                        value: 0
                    },
                    recommond: {
                        isRequire: false,
                        type: 'int',
                        key: 'recommond',
                        value: 1
                    },
                    goodsItem: {
                        isRequire: true,
                        type: 'array>GoodsItem',
                        key: 'goodsItem',
                        value: []
                    }
                }
            default:
                break
        }
    },
    load: async (type = 'update', json) => {
        switch (type) {
            case 'update':
                const { data } = await Goods.API('info', { params: { id: json['id'] }, data: {} })
                return { res: data, form: formToolkit.fill(Goods.FORM.init('update'), data) }
            default:
                break
        }
    }
}

Goods.API = (type = 'list', req) => {
    switch (type) {
        case 'list':
            return new Promise((resolve) => {
                api.goods.list(req.params, req.data).then(res => {
                    const { status, data, page } = res
                    if (status) {
                        resolve({ data, page })
                    }
                })
            })
        case 'add':
            return new Promise((resolve) => {
                api.goods.add(req.params, req.data).then(res => {
                    const { status, data } = res
                    if (status) {
                        resolve({ data })
                    }
                })
            })
        case 'info':
            return new Promise((resolve) => {
                api.goods.info(req.params, req.data).then(res => {
                    const { status, data } = res
                    if (status) {
                        resolve({ data })
                    }
                })
            })
        case 'update':
            return new Promise((resolve) => {
                api.goods.update(req.params, req.data).then(res => {
                    const { status, data } = res
                    if (status) {
                        resolve({ data })
                    }
                })
            })
        default:
            break
    }
}